/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers.changePassword;

import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpServletRequest;
import javax.sql.*;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 *
 * @author Ida
 */
@FacesValidator("javaclass.OldPasswordValidator")
public class OldPasswordValidator implements Validator {
    @Resource(name = "jdbc/sysprosjresource")
    private DataSource dataSource;
    private Connection conn = null;
    private PreparedStatement stmt = null;
    private ResultSet res = null;

     public OldPasswordValidator() {
            try {
                final Context ctx =  new InitialContext();
                dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/sysprosjresource");
            } catch (Exception e) {
                System.out.println("Feil, Oversikt(): " + e);
                throw new RuntimeException(e);
            }
    }

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        ExternalContext eContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) eContext.getRequest();
        String oldPassword = value.toString();
        String username = request.getRemoteUser();
        try {
            conn = dataSource.getConnection();
        } catch (SQLException ex) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    ((HtmlInputSecret)component).getLabel() + " Validation Error: Unable to get connection",
                    ((HtmlInputSecret)component).getLabel() + " Validation Error: No connection"));
        }
        try {
            stmt = conn.prepareStatement("SELECT password FROM users WHERE password=? AND userName=?");
            stmt.setString(1, oldPassword);
            stmt.setString(2, username);
            res = stmt.executeQuery();
            if (!res.next())
             throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    ((HtmlInputSecret)component).getLabel() + " Validation Error: Feil passord",
                    ((HtmlInputSecret)component).getLabel() +" "));

        } catch(SQLException e){
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    ((HtmlInputSecret)component).getLabel() + " Validation Error: SQL error",
                    ((HtmlInputSecret)component).getLabel() + " Validation Error: SQL error extnded"));
        }
        finally{
            controllers.utils.DatabaseHjelper.lukk(res);
            controllers.utils.DatabaseHjelper.lukk(stmt);
            controllers.utils.DatabaseHjelper.lukk(conn);

        }
    }


}
